from rest_framework.serializers import ModelSerializer,StringRelatedField,IntegerField
from apps.questions.models import Label,Question,Reply
from apps.users.models import User



class UserSerializerSimple(ModelSerializer):

    class Meta:
        model = User
        fields = ('id', 'username', 'avatar')


class ReplySerializerForSubAndParent(ModelSerializer):

    user = UserSerializerSimple(read_only=True)

    class Meta:
        model = Reply
        fields = ["id", "content","createtime","useful_count","unuseful_count","user"]


class ReplySerializers(ModelSerializer):

    class Meta:
        model = Reply
        fields = '__all__'



#问题回答序列器
class ReplyQuestionSerializer(ModelSerializer):

    user = UserSerializerSimple(read_only=True)
    subs = ReplySerializerForSubAndParent(read_only=True, many=True)
    parent = ReplySerializerForSubAndParent(read_only=True)

    class Meta:

        model = Reply
        fields = ["id", "content","createtime","useful_count",'problem',"unuseful_count","subs","user","parent"]




#问答列表序列化器
class QuestionListSerializer(ModelSerializer):

    user = StringRelatedField(read_only=True)
    labels = StringRelatedField(read_only=True, many=True)


    class Meta:

        model = Question
        # fields = '__all__'
        fields = ["id","createtime","labels","reply","replyname","replytime","title","unuseful_count","useful_count","user","visits","content"]

#发布问题序列化器
class QuestionPublishSerializer(ModelSerializer):

    class Meta:

        model = Question
        fields = '__all__'



#问题详情序列化器
class QuestionDetailsSerializer(ModelSerializer):

    user = StringRelatedField(read_only=True)
    labels = StringRelatedField(read_only=True, many=True)
    comment_question = ReplyQuestionSerializer(read_only=True, many=True)
    answer_question = ReplyQuestionSerializer(read_only=True, many=True)

    class Meta:

        model = Question
        fields = ["id","createtime","labels","reply","replyname","replytime","title","unuseful_count","useful_count","user","visits","content","comment_question","answer_question"]



#标签列表序列化器
class LabelListSerializer(ModelSerializer):

    class Meta:

        model = Label
        fields = ['id','label_name']


#用户选择标签列表
class LabelUserQuestionListSerializer(ModelSerializer):

    questions = QuestionListSerializer(many=True,read_only=True)

    class Meta:

        model = Label
        fields = ['id','questions','label_name','desc','baike_url','label_icon','users']







